<template>
    <div>
        <el-card style="width: 50%; margin-left: 22%;margin-top: 4%;">
            <el-form :model="user" label-width="80px" style="padding-right: 20px;">
                <div style="margin: 15px; text-align: center;">
                    <el-upload class="avatar-uploader" :action="$baseUrl+'/upload'"
                        :headers="{token:user.token}"
                        :show-file-list="false" 
                        :on-success="handleAvatarSuccess" 
                        :before-upload="beforeAvatarUpload">
                        <img v-if="user.image" :src="user.image" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>

                </div>
                <el-form-Item label="用户名" prop="username">
                    <el-Input v-model="user.username" placeholder="用户名">123</el-Input>
                </el-form-Item>
                <el-form-Item label="姓名" prop="name">
                    <el-Input v-model="user.name" placeholder="姓名"></el-Input>
                </el-form-Item>
                <el-form-Item label="电话" prop="phone">
                    <el-Input v-model="user.phone" placeholder="电话"></el-Input>
                </el-form-Item>
                <el-form-Item label="邮箱" prop="email">
                    <el-Input v-model="user.email" placeholder="邮箱"></el-Input>
                </el-form-Item>
                <el-form-Item label="地址" prop="address">
                    <el-Input type="textarea" v-model="user.address" placeholder="地址"></el-Input>
                </el-form-Item>
                <div style="text-align: center;">
                    <el-button type="primary" @click="update">
                        保存
                    </el-button>
                </div>

            </el-form>
        </el-card>
    </div>

</template>

<script>
export default{
    name:"PersonView",
    data(){
        return{
            user:JSON.parse(localStorage.getItem('user'||'{}')),
        }
    },
    methods:{    
        update(){
            this.$request.put('/user/update',this.user).then(res=>{
                if(res.code=='1'){
                    this.$message.success('保存成功')
                    localStorage.setItem('user',JSON.stringify(this.user))
                    this.$emit('update:user',this.user)
                }
                else{
                    this.$message.error('保存失败')
                }
            })

        },
        handleAvatarSuccess(response,file,fileList) {
            console.log("1");
            console.log(response,file,fileList);
            this.user.image=response.data;
         },
      
        beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
        }
    }

}

</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 50%;
  }



  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
  }
</style>